package com.caigou.modules.tools.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.tools.adapter.TaskListAdapter
import com.caigou.modules.tools.bean.ActiveItemBean
import com.caigou.modules.tools.databinding.FragmentTaskListBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@AndroidEntryPoint
class MedalTaskListFragment : BaseFragment() {

    private var _binding: FragmentTaskListBinding? = null
    private val binding get() = _binding!!

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: TaskListAdapter by lazy {
        TaskListAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentTaskListBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.activeTaskListRecyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.getItem(position)
            if (item.status == 0) {
                when (item.taskId) {
                    14 -> {
                        //TODO vip
                    }
                    15, 17, 19 -> {
                        ARouter.getInstance().build(RouterPath.Famous.PAGE_PUBLISH)
                            .withInt(AppConstants.FAMOUS_TYPE, 1)
                            .navigation()
                    }
                    16, 18, 20 -> {
                        ARouter.getInstance().build(RouterPath.Famous.PAGE_PUBLISH)
                            .withInt(AppConstants.FAMOUS_TYPE, 2)
                            .navigation()
                    }
                    22, 25, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41 -> {
                        LiveDataBus.get().with(BusKey.SWITCH_MAIN_PAGE_TAB).value = 0
                        requireActivity().finish()
                    }
                    28 -> {
                        ARouter.getInstance().build(RouterPath.Tools.PAGE_TOOLS)
                            .withString(AppConstants.TARGET_FRAGMENT_TAG, RouterPath.Tools.TAG_INVITE)
                            .navigation()
                    }
                }
            }
        }
    }

    override fun lazyLoad() {
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    fun setTaskList(list: MutableList<ActiveItemBean>) {
        mAdapter.setList(list)
    }
}